package com.example.verification.utils;

import com.example.verification.model.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @author rcl
 * @date 2019/8/15 15:03
 */
public class SysTools {
    static final String PASSWORD = "123456";//默认初始密码
    static final String HASH_ALGORITH_NAME = "MD5";//加密方式
    static final int HASH_ITERATIONS = 1024;//加密次数
    static final String CREATE_TIME = "setCreateTime";

    /**
     * 获取当前登录用户
     *
     * @param
     * @return com.example.verification.model.User
     * @author rcl
     * @date 2019/8/15 15:04
     */
    public static User getUser() {
        return (User) SecurityUtils.getSubject().getPrincipal();
    }

    /**
     * 对象判空
     *
     * <pre>
     * 判空 Collection、Map、数组、Iterator、Iterable 类型对象中的元素个数是否为 0 规则：
     * 1：null 返回 true
     * 2：List、Set 等一切继承自 Collection 的，返回 isEmpty()
     * 3：Map 返回 isEmpty()
     * 4：数组返回 length == 0
     * 5：Iterator 返回 ! hasNext()
     * 6：Iterable 返回 !
     * iterator().hasNext()
     * </pre>
     */
    public static boolean isEmpty(Object o) {
        if (o == null) {
            return true;
        }
        if (o instanceof String) {
            if (o.toString().equals("")) {
                return true;
            }
        }
        if (o instanceof Collection) {
            return ((Collection<?>) o).isEmpty();
        }
        if (o instanceof Map) {
            return ((Map<?, ?>) o).isEmpty();
        }
        if (o.getClass().isArray()) {
            return Array.getLength(o) == 0;
        }
        if (o instanceof Iterator) {
            return !((Iterator<?>) o).hasNext();
        }
        if (o instanceof Iterable) {
            return !((Iterable<?>) o).iterator().hasNext();
        }
        return false;
    }

    /**
     * 获取UUID
     *
     * @param
     * @return java.lang.String
     * @author rcl
     * @date 2019/8/15 15:16
     */
    public static String getUUID() {
        return String.valueOf(UUID.randomUUID()).replace("-", "");
    }

    /**
     * 根据登录名生成初始密码
     *
     * @param [userName]
     * @return java.lang.String
     * @author rcl
     * @date 2019/8/15 15:21
     */
    public static String getPassword(String userName) {
        ByteSource credentialsSalt = ByteSource.Util.bytes(userName);
        Object obj = new SimpleHash(HASH_ALGORITH_NAME, PASSWORD,
                credentialsSalt, HASH_ITERATIONS);
        return obj.toString();
    }

    /**
     * 设置创建时间，未传递属性名则使用默认的setCreateTime
     *
     * @author rcl
     * @date 2019/8/15 16:42
     * @param  t, fieldName
     * @return void
     */
    public static void setCreatedTime(Object t, String fieldName) {
        Map<String, Object> result = new HashMap<>();

        Class<?> sourceClass = t.getClass();
        //拿到所有的字段,不包括继承的字段
        Field[] fields = sourceClass.getDeclaredFields();
        Method[] methods = sourceClass.getDeclaredMethods();
        try {
            String setName = "get";
            if (isEmpty(fieldName)) {
                setName = CREATE_TIME;
            } else {
                String first = fieldName.substring(0, 1).toUpperCase();
                String after = fieldName.substring(1);
                setName = setName + first + after;
            }
            for (Method method : methods) {
                if (method.getName().equals(setName)) {
                    method.invoke(t, new Date());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
